import { apiService } from './api'
import { API_ENDPOINTS } from '@/config/api'

// 固件接口
export interface Firmware {
  id: string
  version: string
  signature: string
  release_time: string
  product_id: string
  description: string
  download_url: string
  file_size: number
  created_at: string
  updated_at: string
  product: {
    id: string
    name: string
    public_id: string
  }
}

// 创建固件请求
export interface CreateFirmwareRequest {
  version: string
  product_id: string
  description?: string
  file: File | null
}

// 固件服务
export const firmwareService = {
  // 获取固件列表
  getFirmwares: (productId?: string): Promise<Firmware[]> => {
    const params = productId ? { product_id: productId } : undefined
    return apiService.get<Firmware[]>(API_ENDPOINTS.FIRMWARES, params)
  },

  // 获取单个固件
  getFirmware: (id: string): Promise<Firmware> => {
    return apiService.get<Firmware>(API_ENDPOINTS.FIRMWARE(id))
  },

  // 创建固件
  createFirmware: (data: CreateFirmwareRequest): Promise<Firmware> => {
    const formData = new FormData()
    formData.append('version', data.version)
    formData.append('product_id', data.product_id)
    if (data.description) {
      formData.append('description', data.description)
    }
    if (data.file) {
      formData.append('file', data.file)
    }
    
    return apiService.upload<Firmware>(API_ENDPOINTS.FIRMWARES, formData)
  },

  // 删除固件
  deleteFirmware: (id: string): Promise<{ message: string }> => {
    return apiService.delete<{ message: string }>(API_ENDPOINTS.FIRMWARE(id))
  },

  // 下载固件
  downloadFirmware: (filename: string): string => {
    return `${API_ENDPOINTS.FIRMWARE_DOWNLOAD(filename)}`
  }
} 